package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"user_center/models/mysql"

	"user_center/userrpc/internal/svc"
	"user_center/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PerfectLogic {
	return &PerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 用户信息完善
func (l *PerfectLogic) Perfect(in *userrpc.PerfectRequest) (*userrpc.PerfectResponse, error) {
	// todo: add your logic here and delete this line
	var users mysql.User
	mysql.DB.Debug().Where("id = ?", in.Id).Find(&users)
	if users.ID == 0 {
		return nil, fmt.Errorf("当前用户不存在，无法完善个人信息")
	}

	user := mysql.User{
		Model:   gorm.Model{ID: uint(in.Id)},
		Name:    in.Name,
		Sex:     in.Sex,
		Address: in.Address,
		Email:   in.Email,
	}
	mysql.DB.Debug().Updates(&user)

	return &userrpc.PerfectResponse{
		Id: int64(user.ID),
	}, nil
}
